/** Lachlan Hewson-Doherty (h201384171)
 *  Diploma of Digital and Interactive Games
 *  lachlan_doherty@live.com.au
 *  2013
 */

package cabbage.database.operations;



import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import cabbage.database.utilities.DatabaseConnection;



public class AccountOps {
	
	
	
	
	
	
	private DatabaseConnection db;
	
	
	
	
	
	
	
	
	public boolean checkEmailAddressExists(String emailaddress) throws SQLException {

		db = new DatabaseConnection
								();
		
		Connection conn = db.getConnection
									();		
		
		PreparedStatement ps = conn.prepareStatement
								("SELECT emailaddress FROM account WHERE emailaddress=?");
		
		ps.setString
				(1, emailaddress);
		
		ResultSet rs = ps.executeQuery
									();
		
		if (rs.next()) {	
			
		//	System.out.println("Checking for ( " + emailaddress + " ) ... ... ... Found! " + rs);
			conn.close();
			return true;
			
		}
		
	//	System.out.println("Checking for ( " + emailaddress + " ) ... ... ... Not Found! ");
		conn.close();		
		return false;

	}
	
	
	
	
	
	
	public boolean login(String username, String password) throws SQLException {

		db = new DatabaseConnection
								();
		
		Connection conn = db.getConnection
									();
		
		PreparedStatement ps = conn.prepareStatement
								("SELECT username,password FROM account WHERE username=? AND password=?");
		
		ps.setString
				(1, username);
		
		ps.setString
				(2, password);
		
		ResultSet rs = ps.executeQuery
										();

		if (rs.next()) {
			conn.close();
			return true;
		
		}
		
		conn.close();
		return false;
	
	}
	

}
